﻿
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Easy.Domain.Core.Specification
{

    public sealed class NotSpecification<T> : Specification<T> where T : class
    {
        Expression<Func<T, bool>> _OriginalCriteria;

        public NotSpecification(ISpecification<T> originalSpecification)
        {
            Easy.Common.CheckHelper.NotNull(originalSpecification, "originalSpecification");

            _OriginalCriteria = originalSpecification.SatisfiedBy();
        }

        public NotSpecification(Expression<Func<T, bool>> originalSpecification)
        {
            Easy.Common.CheckHelper.NotNull(originalSpecification, "originalSpecification");

            _OriginalCriteria = originalSpecification;
        }

        public override Expression<Func<T, bool>> SatisfiedBy()
        {

            return Expression.Lambda<Func<T, bool>>(Expression.Not(_OriginalCriteria.Body),
                                                         _OriginalCriteria.Parameters.Single());
        }


    }
}
